package com.sicau.socialpracticesystem.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sicau.socialpracticesystem.common.constant.UrlConstants;
import com.sicau.socialpracticesystem.common.returning.ResultData;
import com.sicau.socialpracticesystem.config.aop.ValidateAspect;
import com.sicau.socialpracticesystem.entity.param.ValidList;
import com.sicau.socialpracticesystem.entity.param.prize.PrizeAddParam;
import com.sicau.socialpracticesystem.entity.param.prize.PrizeExchangingParam;
import com.sicau.socialpracticesystem.entity.param.prize.PrizeQueryParam;
import com.sicau.socialpracticesystem.entity.param.prize.PrizeUpdateParam;
import com.sicau.socialpracticesystem.entity.po.Prize;
import com.sicau.socialpracticesystem.entity.vo.PageDataVO;
import com.sicau.socialpracticesystem.entity.vo.prize.PrizeVO;
import com.sicau.socialpracticesystem.service.PrizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.dozermapper.core.Mapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 奖品表 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@RestController
@Api(tags = "奖品相关接口")
@PreAuthorize("isAuthenticated()")
@RequestMapping(UrlConstants.PRIZE_PREFIX)
public class PrizeController {

    @Resource
    private PrizeService prizeService;

    @Resource
    private Mapper dozerMapper;

    @PostMapping
    @ApiOperation("添加一个或多个奖品")
    @ValidateAspect.ParamValidate()
    public ResultData<Object> addPrizes(@RequestBody @Valid ValidList<PrizeAddParam> prizeAddParam,
                                        @ApiParam(hidden = true)
                                        @SuppressWarnings("unused") BindingResult bindingResult) {
        List<Prize> prizes = prizeAddParam.stream().map(item -> dozerMapper.map(item, Prize.class))
                .collect(Collectors.toList());
        return prizeService.saveBatch(prizes)
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED();
    }

    @GetMapping
    @PreAuthorize("permitAll()")
    @ApiOperation("查询奖品")
    public ResultData<PageDataVO<PrizeVO>> getPrizes(PrizeQueryParam prizeQueryParam) {
        PageDataVO<PrizeVO> prizePageDataVO = prizeService.queryPrizeByConditions(prizeQueryParam);
        return new ResultData<PageDataVO<PrizeVO>>().OK().setObj(prizePageDataVO);
    }

    @PutMapping("/{id}")
    @ApiOperation("修改奖品")
    @ValidateAspect.ParamValidate()
    public ResultData<Object> updatePrize(@PathVariable String id, @RequestBody @Valid PrizeUpdateParam prizeUpdateParam) {
        Prize prize = prizeService.getById(id);
        Long prizeNumber = prizeUpdateParam.getPrizeNumber();
        if (prizeNumber != null) {
            long left = prizeNumber - prize.getExchangedNumber();
            if (left < 0) {
                return new ResultData<>().FAILED().setResultIns("奖品总数量不能小于已经兑换的数量");
            }
        }
        dozerMapper.map(prizeUpdateParam, prize);
        return prizeService.updateById(prize)
                ? new ResultData<>().OK().setObj(prizeService.queryPrizeById(id))
                : new ResultData<>().FAILED();
    }

    @DeleteMapping
    @ApiOperation("删除一个或多个奖品")
    public ResultData<Object> removePrizes(@RequestParam("ids") ArrayList<Integer> ids) {
        return prizeService.removeByIds(ids)
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED().setResultIns("部分或全部数据删除失败, 可能有的数据已经被删除了");
    }

    @PutMapping("exchanging/{id}")
    @ApiOperation("设置奖品是否上架")
    public ResultData<Object> setExchanging(@PathVariable("id") String id, @RequestBody PrizeExchangingParam param) {
        if (!(param.getOnExchanging() == 0 || param.getOnExchanging() == 1)) {
            return new ResultData<>().FAILED().setResultIns("不能设置除0、1以外的flag");
        }
        boolean update = prizeService.update(new LambdaUpdateWrapper<Prize>()
                .eq(Prize::getId, id)
                .set(Prize::getOnExchanging, param.getOnExchanging()));
        return update
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED();
    }


}

